package com.ruoyi.system.coretools;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import com.ruoyi.common.annotation.LanguageType;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.name;

@Component
public class LanguageUtil {

    @Resource
    private RedisCache redisCache;
    @Resource
    private TranslateDriver translateDriver;
    @Resource
    private HttpServletRequest request;
    public <T> void LanguageValue(List<T> objs){
        for (T obj : objs) {
            LanguageResult(obj);
        }
    }

    public <T> void LanguageResult2(T obj){
        String currCode = request.getHeader("currCode");
        if("zh".equals(currCode)){
            return;
        }
        Class<?> aClass = obj.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);//新加的
            LanguageType annotation = declaredField.getAnnotation(LanguageType.class);
            if (annotation == null) {
                continue;
            }
            try {
                Object o = declaredField.get(obj);
                if (Objects.isNull(o)){
                    continue;
                }
                String sourceValue = o.toString();
                String value = redisCache.getCacheObject(currCode+sourceValue);
            if (StringUtils.isEmpty(value)) {
                value = translateDriver.callTranslate("zh", currCode, sourceValue);
                // 调用多语言工具，并完成数据保存
                redisCache.setCacheObject(currCode+sourceValue, value);
            }
                declaredField.set(obj,value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }


    public <T> void LanguageResult(T obj){
        return;
//        String currCode = request.getHeader("currCode");
//        if("zh".equals(currCode)){
//            return;
//        }
//        Class<?> aClass = obj.getClass();
//        Field[] declaredFields = aClass.getDeclaredFields();
//        for (Field declaredField : declaredFields) {
//            declaredField.setAccessible(true);//新加的
//            if (String.class.equals(declaredField.getType())){
//                try {
//                    if (String.class.equals(declaredField.getType()) ) {
//                        Object o = declaredField.get(obj);
//                        if (Objects.isNull(o)||StringUtils.isEmpty(o.toString())){
//                            continue;
//                        }
//                        String sourceValue = o.toString();
//                        String value = redisCache.getCacheObject(currCode+sourceValue);
//                        if (StringUtils.isEmpty(value)) {
//                            value = translateDriver.callTranslate("zh", currCode, sourceValue);
//                            // 调用多语言工具，并完成数据保存
//                            redisCache.setCacheObject(currCode+sourceValue, value);
//                        }
//                        declaredField.set(obj,value);
//                    }
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//
//        }
    }


    /**
     * 1、通过注解或获取到需要转义的数据
     * 2、把每个转义的数据都得到各个国家的文字数据并保存到redis中
     * @param obj
     */
    public void LanguageAdd(Object obj){
        String currCode = request.getHeader("currCode");
        Class<?> aClass = obj.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);//新加的
            LanguageType annotation = declaredField.getAnnotation(LanguageType.class);
            if (annotation != null) {
                continue;
            }
            declaredField.setAccessible(true);
            try {
                Object o = declaredField.get(obj);
                if (Objects.isNull(o)){
                    continue;
                }
                String sourceValue = o.toString();
                String value = redisCache.getCacheObject(currCode+sourceValue);
            if (StringUtils.isEmpty(value)) {

                // 调用多语言工具，并完成数据保存
                value = "工具方法";
            }

                declaredField.set(obj,value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
